package com.lgx.group.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lgx.common.entity.Dictionary;
import com.lgx.common.web.Constants;
import com.lgx.common.web.Pagination;
import com.lgx.common.web.util.DictUtils;
import com.lgx.right.entity.User;
import com.lgx.right.entity.UserExpand;
import com.lgx.right.service.IUserExpandService;
import com.lgx.right.service.IUserService;
import com.lgx.right.vo.UserVO;

@Controller
@RequestMapping("/home")
public class HomeController {
	@Autowired
	private IUserService userService;
	
	public IUserService getUserService() {
		return userService;
	}
	
	@Autowired
	private IUserExpandService userExpandService;

	public IUserExpandService getUserExpandService() {
		return userExpandService;
	}
	
	@RequestMapping("/index.htm")
	public String index(HttpServletRequest request,HttpServletResponse response){
		return "../../index";
	}

	@RequestMapping("/main.ftl")
	public String main(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		
		request.setAttribute("basePath", basePath);
		
		//会员等级
		List<Dictionary> userDegrees = DictUtils.getDictListByKind(DictUtils.USER_DEGREE);
		request.setAttribute("userDegrees", userDegrees);
		
		//获取用户列表
		String pageNum = request.getParameter("page");
		String pageSize= request.getParameter("rows");
		
		if(StringUtils.isEmpty(pageSize))pageSize = Constants.PAGE_SIZE;
		if(StringUtils.isEmpty(pageNum))pageNum = Constants.FIRST_PAGE_NUM;
		
		Object[] params = new Object[10];
		String sex = request.getParameter("sex");
		String degree = request.getParameter("degree");
		
		if(!StringUtils.isEmpty(sex) && !"-1".equals(sex))
		{
			params[0] = sex;
		}else{
			params[0] = "";
		}
		
		if(!StringUtils.isEmpty(degree) && !"-1".equals(degree))
		{
			params[1] = degree;
		}else{
			params[1] = "";
		}
		
		Pagination userVOPage = this.userService.findUserPagination(params, UserVO.class, Integer.parseInt(pageSize), Integer.parseInt(pageNum));
		request.setAttribute("userVOPage", userVOPage);
		
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("sex", sex);
		request.setAttribute("degree", degree);
		
		return "home/main";
	}
	
	@RequestMapping("/login.htm")  
	public String login(HttpServletRequest request,HttpServletResponse response) {
		//用户登录
		String username = request.getParameter("username");
		String password = request.getParameter("passwordMD5");
		String redirectUrl = request.getParameter("redirectUrl");
		
		User user = userService.loginUser(username, password);
		if(user == null){
			request.setAttribute("errorMsg", "用户和密码错误！");
//			return "../../index";
			return redirectUrl;
		}
		
		request.getSession().setAttribute(Constants.LOGIN_USER, user);
		
		UserExpand userExpand = this.userExpandService.findUserExpandByUserId(user.getId());
		if(userExpand == null){
			request.setAttribute("errorMsg", "该用户没有扩展信息");
//			return "../../index";
			return redirectUrl;
		}
		request.getSession().setAttribute(Constants.LOGIN_USEREXPAND, userExpand);
		
//		return "../../index";
		return redirectUrl;
	}
	
	@RequestMapping("/logout.htm")  
	public String logout(HttpServletRequest request,HttpServletResponse response) {
		String redirectUrl = request.getParameter("redirectUrl");
		
		request.getSession().removeAttribute(Constants.LOGIN_USER);
		request.getSession().removeAttribute(Constants.LOGIN_USEREXPAND);
		
//		return "../../index";
		return redirectUrl;
	}
	
	@RequestMapping("/userSearch.htm")  
	public String userSearch(HttpServletRequest request,HttpServletResponse response) {
		String pageNum = request.getParameter("page");
		String sex = request.getParameter("sex");
		String degree = request.getParameter("degree");
		
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("sex", sex);
		request.setAttribute("degree", degree);
		return "../../index";
	}
}
